import service
import copy
import entityCommon

class EntityRecipeService(service.Service):
    __guid__ = 'svc.entityRecipeSvc'
    __exportedcalls__ = {'GetTypeRecipe': [service.ROLE_ANY],
     'GetGroupRecipe': [service.ROLE_SERVICE],
     'GetCategoryRecipe': [service.ROLE_SERVICE]}
    __notifyevents__ = []

    def __init__(self):
        service.Service.__init__(self)



    def Run(self, *args):
        self.typeRecipes = {}
        self.groupRecipes = {}
        self.categoryRecipes = {}



    def GetInventoryGroup(self, groupID):
        return cfg.invgroups.Get(groupID)



    def GetInventoryType(self, typeID):
        return cfg.invtypes.Get(typeID)



    def GetTypeRecipe(self, typeID, overrideDict = None):
        if (typeID not in self.typeRecipes):
            recipe = self._LoadTypeRecipe(typeID)
        else:
            recipe = copy.deepcopy(self.typeRecipes[typeID])
        if (overrideDict is not None):
            self._IntegrateDicts(recipe, overrideDict)
        return recipe



    def GetGroupRecipe(self, groupID, overrideDict = None):
        if (groupID not in self.groupRecipes):
            recipe = self._LoadGroupRecipe(groupID)
        else:
            recipe = copy.deepcopy(self.groupRecipes[groupID])
        if (overrideDict is not None):
            self._IntegrateDicts(recipe, overrideDict)
        return recipe



    def GetCategoryRecipe(self, categoryID, overrideDict = None):
        if (categoryID not in self.categoryRecipes):
            recipe = self._LoadCategoryRecipe(categoryID)
        else:
            recipe = copy.deepcopy(self.categoryRecipes[categoryID])
        if (overrideDict is not None):
            self._IntegrateDicts(recipe, overrideDict)
        return recipe



    def _IntegrateDicts(self, recipe, overrideDict):
        for (componentID, overrides,) in overrideDict.iteritems():
            if (componentID not in recipe):
                recipe[componentID] = overrides
            else:
                for (overrideKey, overrideValue,) in overrides.iteritems():
                    recipe[componentID][overrideKey] = overrideValue





    def _LoadInitValues(self, recipeComponent, ingredientID):
        cachedInitVals = cfg.entityIngredientInitialValues
        if (ingredientID not in cachedInitVals):
            return 
        initvals = cachedInitVals[ingredientID]
        for row in initvals:
            val = entityCommon.GetIngredientInitialValue(row)
            recipeComponent[row.initialValueName] = val




    def _LoadCategoryRecipe(self, categoryID):
        recipe = {}
        ingredientCache = cfg.entityIngredientsByParentID
        if (categoryID not in ingredientCache):
            return recipe
        else:
            ingredients = ingredientCache[categoryID]
            for row in ingredients:
                if (row.parentType != entityCommon.PARENT_CATEGORYID):
                    continue
                recipe[row.componentID] = {}
                self._LoadInitValues(recipe[row.componentID], row.ingredientID)

            self.categoryRecipes[categoryID] = copy.deepcopy(recipe)
            return recipe



    def _LoadGroupRecipe(self, groupID):
        groupObj = self.GetInventoryGroup(groupID)
        if (groupObj.categoryID not in self.categoryRecipes):
            recipe = self._LoadCategoryRecipe(groupObj.categoryID)
        else:
            recipe = copy.deepcopy(self.categoryRecipes[groupObj.categoryID])
        ingredientCache = cfg.entityIngredientsByParentID
        if (groupID not in ingredientCache):
            return recipe
        else:
            ingredients = ingredientCache[groupID]
            for row in ingredients:
                if (row.parentType != entityCommon.PARENT_GROUPID):
                    continue
                if (row.componentID not in recipe):
                    recipe[row.componentID] = {}
                self._LoadInitValues(recipe[row.componentID], row.ingredientID)

            self.groupRecipes[groupID] = copy.deepcopy(recipe)
            return recipe



    def _LoadTypeRecipe(self, typeID):
        recipe = {}
        typeObj = self.GetInventoryType(typeID)
        if (typeObj.groupID not in self.groupRecipes):
            recipe = self._LoadGroupRecipe(typeObj.groupID)
        else:
            recipe = copy.deepcopy(self.groupRecipes[typeObj.groupID])
        ingredientCache = cfg.entityIngredientsByParentID
        if (typeID in ingredientCache):
            ingredients = ingredientCache[typeID]
            for row in ingredients:
                if (row.parentType != entityCommon.PARENT_TYPEID):
                    continue
                if (row.componentID not in recipe):
                    recipe[row.componentID] = {}
                self._LoadInitValues(recipe[row.componentID], row.ingredientID)

        for (componentID, initValues,) in recipe.iteritems():
            initValues['_typeID'] = typeID

        self.typeRecipes[typeID] = recipe
        return recipe




